package com.xilihui.xlh.core.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.HomeActivity;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/16.
 */

public abstract class BaseCompatActivity extends RxAppCompatActivity {
    
    
    private TextView tvTitle;
    private ActionBar supportActionBar;
    private OnClickRightImgListener onClickRightImgListener;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YEventBuses.register(this);
        setContentView(getLayout());
        ButterKnife.bind(this);
        
        initToolbar();
        initUI();
        initUI(savedInstanceState);
        initData();
    }
    
    public void initToolbar(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        if(toolbar !=null){
            toolbar.setTitle("");
            tvTitle = new TextView(this);
            tvTitle.setSingleLine();
            tvTitle.setEllipsize(TextUtils.TruncateAt.END);
            tvTitle.setGravity(Gravity.CENTER);
            tvTitle.setTextSize(20);//20sp
            tvTitle.setTextColor(getResources().getColor(R.color.text));
            tvTitle.setText(initTitle());
            Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT,
                    Toolbar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
            toolbar.addView(tvTitle,layoutParams);
            
            setSupportActionBar(toolbar);
            supportActionBar = getSupportActionBar();
            if(supportActionBar != null){
                supportActionBar.setDisplayHomeAsUpEnabled(true);
                supportActionBar.setHomeAsUpIndicator(R.mipmap.ic_arrow_back);
            }
            
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        //友盟推送
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        if(tvTitle != null){
            tvTitle.setText(title);
        }
    }
    
    public void setTitleColorRes(int colorRes){
        if(tvTitle != null){
            tvTitle.setTextColor(getResources().getColor(colorRes));
        }
    }

    public void setHomeAsUpIndicator(@DrawableRes int drawableRes) {
        if (supportActionBar != null) {
            supportActionBar.setHomeAsUpIndicator(drawableRes);
        }
    }
    
    
    public abstract int getLayout();
    
    public abstract String initTitle();
    //初始UI
    public abstract void initUI(@Nullable Bundle savedInstanceState);
    
    public abstract void initData();
    
    public void initUI(){
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: // 顶栏返回键
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void setOnClickRightImgListener(OnClickRightImgListener onClickRightImgListener){
        this.onClickRightImgListener = onClickRightImgListener;
    }
    
    public interface OnClickRightImgListener{
        public void OnClick();
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        if(event.is("ReLogin")){
            finish();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        YEventBuses.unregister(this);
    }
}
